package com.eight.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.AclPermission;
import com.eight.common.pojo.AclRolePermission;
import com.eight.common.pojo.AclUser;
import com.eight.common.vo.Result;
import com.eight.system.dao.AclPermissionDao;
import com.eight.system.dao.RolePermissionDao;
import com.eight.system.service.PermissionService;
import com.eight.system.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.column;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/23 20:57
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private AclPermissionDao permissionDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Override
    public List<AclPermission> findByUserId(String userId) {
        List<AclPermission> permissionList=permissionDao.selectByUserId(userId);
        //1.所有一级菜单
        List<AclPermission> firsts=new ArrayList<>();
        for(AclPermission permission:permissionList){
            if(permission.getPid().equals("1")){
                firsts.add(permission);
            }
        }
        //2.查询二级菜单
        for(AclPermission first:firsts){
            List<AclPermission> children = findChildren(first.getId(), permissionList);
            first.setChildren(children);
        }
        return firsts;
    }

    @Override
    public List<AclPermission> findAll() {
        List<AclPermission> permissionList = permissionDao.selectList(null);
        //1.所有一级菜单
        List<AclPermission> firsts=new ArrayList<>();
        for(AclPermission permission:permissionList){
            if(permission.getPid().equals("1")){
                firsts.add(permission);
            }
        }
        //2.查询二级菜单
        for(AclPermission first:firsts){
            List<AclPermission> children = findChildren(first.getId(), permissionList);
            first.setChildren(children);
        }
        return firsts;
    }
    @Override
    public List<String> findByRoleId(String roleId) {
        QueryWrapper<AclRolePermission> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<AclRolePermission> rolePermissions = rolePermissionDao.selectList(wrapper);
        List<String> collect = rolePermissions.stream().map(item -> item.getPermissionId()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void confirm(String roleId, String[] permissionIds) {
        //删除该角色具有的权限
        UpdateWrapper<AclRolePermission> wrapper=new UpdateWrapper<>();
        wrapper.eq("role_id",roleId);
        rolePermissionDao.delete(wrapper);
        //批量添加角色的权限
        List<AclRolePermission> permissions= Arrays.stream(permissionIds).map(id->new AclRolePermission(IdUtil.getSnowflakeNextId()+"",roleId,id,0, LocalDateTime.now(),LocalDateTime.now())).collect(Collectors.toList());
        rolePermissionDao.batchInsert(permissions);
    }

    @Override
    public Page<AclPermission> findPage(Integer currentSize, Integer pageSize, PermissionVo permissionVo) {
        Page<AclPermission> pageAclPermission = new Page<>(Integer.valueOf(currentSize), Integer.valueOf(pageSize));
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", "1");
        if (permissionVo.getName() != null && permissionVo.getName() != "") {
            wrapper.like("name", permissionVo.getName());

        }

        Page<AclPermission> aclPermissionPage = permissionDao.selectPage(pageAclPermission, wrapper);
        List<AclPermission> list = permissionDao.selectList(null);
        //1.所有子菜单
        for (AclPermission aclPermission : aclPermissionPage.getRecords()) {
            List<AclPermission> arrayList = this.findChildren(aclPermission.getId(), list);
            aclPermission.setChildren(arrayList);
        }
        return aclPermissionPage;

    }

    @Override
    public int insert(AclPermission aclPermission) {
        int insert = permissionDao.insert(aclPermission);
        return insert;
    }

   @Override
    public int deleteById(List ids) {
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        if (ids != null){
            wrapper.eq("id",ids);
        }

        int delete = permissionDao.deleteBatchIds(ids);
        return delete;
    }

    @Override
    public int update(AclPermission aclPermission) {
        int update = permissionDao.updateById(aclPermission);
        return update;
    }

    private List<AclPermission> findChildren(String pid,List<AclPermission> permissions){
        List<AclPermission> children=new ArrayList<>();

        for(AclPermission p:permissions){
            if(p.getPid().equals(pid)){
                children.add(p);
            }
        }

        for(AclPermission p:children){
            p.setChildren(findChildren(p.getId(),permissions));
        }
        return children;
    }




}
